using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using OpenTK.Math;

namespace Dogfight2008.Common
{
  public class CubeCreator
  {
    public static ColoredTriangleSoup Generate(Color color, float side)
    {
      ColoredTriangleSoup soup = new ColoredTriangleSoup();
      AddUnitCube(soup);
      soup.Map(delegate(ColoredTriangle triangle)
      {
        ColoredTriangle newTri = ScaleTriangle(triangle, side);
        return new ColoredTriangle(color, newTri.Triangle);
      });
      //for (int i = 0; i < 16; i++)
      //{
      //  ColoredTriangle tri = soup[i];
      //  ColoredTriangle scaledTri = ScaleTriangle(tri, side);
      //  soup[i] = scaledTri;
      //}
      return soup;
    }

    private static ColoredTriangle ScaleTriangle(ColoredTriangle tri, float side)
    {
      return new ColoredTriangle(
        tri.Color,
        new Triangle(
            Vector3.Mult(tri.Triangle.P1, side),
            Vector3.Mult(tri.Triangle.P2, side),
            Vector3.Mult(tri.Triangle.P3, side)));
    }

    private static void AddUnitCube(ColoredTriangleSoup soup)
    {
      AddUnitBottom(soup);
      AddUnitTop(soup);
      AddUnitLeft(soup);
      AddUnitRight(soup);
      AddUnitFront(soup);
      AddUnitBack(soup);
    }

    private static void AddUnitFront(ColoredTriangleSoup soup)
    {
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 1, 1), v(1, 0, 1), v(1, 1, 1))));
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 1, 1), v(0, 0, 1), v(1, 0, 1))));
    }

    private static void AddUnitBack(ColoredTriangleSoup soup)
    {
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 1, 0), v(1, 1, 0), v(1, 0, 0))));
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 1, 0), v(1, 0, 0), v(0, 0, 0))));
    }

    private static void AddUnitLeft(ColoredTriangleSoup soup)
    {
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 0, 0), v(0, 1, 1), v(0, 1, 0))));
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 0, 0), v(0, 0, 1), v(0, 1, 1))));
    }

    private static void AddUnitRight(ColoredTriangleSoup soup)
    {
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(1, 0, 0), v(1, 1, 0), v(1, 1, 1))));
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(1, 0, 0), v(1, 1, 1), v(1, 0, 1))));
    }

    private static void AddUnitBottom(ColoredTriangleSoup soup)
    {
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 0, 0), v(1, 0, 0), v(1, 0, 1))));
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 0, 0), v(1, 0, 1), v(0, 0, 1))));
    }

    private static void AddUnitTop(ColoredTriangleSoup soup)
    {
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 1, 0), v(1, 1, 1), v(1, 1, 0))));
      soup.Add(new ColoredTriangle(
        Color.White,
        new Triangle(v(0, 1, 0), v(0, 1, 1), v(1, 1, 1))));
    }

    private static Vector3 v(int x, int y, int z)
    {
      return new Vector3(x, y, z);
    }

  }
}
